<template>
    <BaseMainSlot>
        <template v-slot:searchbox>
            <el-collapse v-model="activeName" accordion @change="handleChange">
                <el-collapse-item name="1">
                    <template slot="title">
                        <div class="select-btns search-btns" @click.stop="changeSelect()">
                            <i class="el-icon-circle-check"></i>
                            <p v-if="activeName === '1'">收起筛选</p>
                            <p v-if="activeName === ''">展开筛选</p>
                        </div>
                    </template>
                    <div class="search-btn search-btns" @click.stop="queryPageList()">
                        <i class="el-icon-circle-check"></i>
                        <p>查询</p>
                    </div>
                    <div class="select-box-content">
                        <div class="select-box-content-name">批次编号</div>
                        <el-input v-model="mnecode" clearable />
                    </div>
                    <div class="select-box-content">
                        <div class="select-box-content-name">鱼塘名称</div>
                        <el-select filterable clearable v-model="fishPondId" filterable clearable>
                            <el-option v-for="(item, index) in pondList" :key="index" :label="item.pondName"
                                :value="item.fishPondId" />
                        </el-select>
                    </div>
                    <div class="select-box-content">
                        <div class="select-box-content-name">作业人</div>
                        <el-select filterable clearable v-model="staffName" filterable clearable>
                            <el-option v-for="(item, index) in staffList" :key="index" :label="item.staffName"
                                :value="item.staffName" />
                        </el-select>
                    </div>
                    <div class="select-box-content">
                        <div class="select-box-content-name">经手日期</div>
                        <el-date-picker v-model="inputDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
                            clearable />
                    </div>
                </el-collapse-item>
            </el-collapse>
        </template>
        <template v-slot:btnsBar>
            <OperationBar :categoryList="operationList" @categoryBar="operationChange" />
        </template>
        <template v-slot:categoryBox>
            <CategoryBar :categoryList="categoryList" @categoryBar="categoryBar" />
        </template>
        <template v-slot:content>
            <el-table ref="tableData" v-loading="loading" highlight-current-row :data="tableData" class="margin-top10px"
                stripe border height="calc(100% - 50px)" :row-key="row => {
                        return row.hangNetWorkId;
                    }
                    " @selection-change="handleSelectionChange" @select="select" @select-all="selectAll">
                <!-- <el-table-column type="selection" :selectable="selectable" width="55" :reserve-selection="true" /> -->
                <el-table-column type="index" label="序号" width="50">
                    <template slot-scope="scoped">
                        {{ scoped.row.hangNetWorkId ? scoped.$index + 1 : '' }}
                    </template>
                </el-table-column>
                <el-table-column prop="pondName" label="鱼塘名称" />
                <el-table-column prop="batchName" label="投入批次" />
                <el-table-column prop="position" label="位置" />
                <el-table-column prop="season" label="季节" />
                <el-table-column prop="staffName" label="经手人" />
                <el-table-column prop="workBeginTime" label="作业开始日期" />
                <el-table-column prop="auditType" v-if="auditType !== 'PASS'" label="操作" width="150">
                    <template slot-scope="scoped">
                        <div class="table-btns-box" v-show="scoped.row.auditType == 'REVIEW'">
                            <div class="table-btns edit-btns" @click="showAuditDialog(scoped.row, 'pass')">
                                <p>通过</p>
                            </div>
                            <div class="table-btns edit-btns" @click="showAuditDialog(scoped.row, 'unpass')">
                                <p>不通过</p>
                            </div>
                        </div>
                        <div class="table-btns-box" v-show="scoped.row.auditType == 'NOT_PASS'">
                            <div class="table-btns edit-btns" @click="showAuditDialog(scoped.row, 'unpass')">
                                <p>查看未通过原因</p>
                            </div>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
                @changeCurrentPage="changeCurrentPage" />
            <AddEditDialog :nowChanceStau="nowChanceStau" :dialogParams="dialogParams" :show="showDialog"
                @cancelDialog="cancelPassDialog" @submitAudite="submitAudite" />
        </template>
    </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import CategoryBar from "@/components/CategoryBar";
import OperationBar from "./components/CategoryBar.vue";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import HangingOperationApi from "@/api/operation-management/hanging-operation";
import EnumApi from "@/api/enum";
import BatchMangeApi from "@/api/batch-mange";
import MaterialApi from "@/api/basic-info-manage/material";
import FishPondApi from "@/api/basic-info-manage/fishPond";
import StaffApi from "@/api/basic-info-manage/staff";
export default {
    name: "WaterChangeOperation",
    components: {
        ActionBar,
        Pagination,
        BaseMainSlot,
        CategoryBar,
        AddEditDialog,
        OperationBar,
    },
    mixins: [mymixin],
    data () {
        return {
            auditedId: "",
            dialogParams: {},
            auditType: "REVIEW", //审核状态
            success: "", //审核是否成功
            mnecode: "", //批次编号
            fishPondId: "", //养殖鱼塘ID
            materialId: "", //经手人
            staffName: "", //经手人
            inputDate: "", //经手日期
            checkedIdList: [],
            pondList: [], //全部鱼塘
            batchList: [], //全部未完成批次
            FishVarietyData: [], //全部品种
            staffList: [], //全部员工/经手人
            materialList: [], //物料
            //分类列表
            categoryList: [],
            showPictureDialog: false,
            showDialog: false,
            //分类列表
            operationList: [
                {
                    id: 0,
                    mark: "router",
                    name: "吊网作业",
                    value: "/handing-operation-audit",
                },
                {
                    id: 1,
                    mark: "router",
                    name: "露网作业",
                    value: "/dew-net-operation-audit",
                },
                {
                    id: 2,
                    mark: "router",
                    name: "投喂作业",
                    value: "/feeding-operation-audit",
                },
            ],
            // activeName: '1',//控制是否默认展开
        };
    },
    computed: {
        ...mapGetters({
            judgePermission: "permission/judgePermission",
        }),
    },
    watch: {},
    created () {
        this.queryPageList();
    },
    mounted () {
        this.queryFishPond();
        this.queryBatchMange();
        this.queryStaff();
        this.queryMaterial();
        this.queryEnumList();
    },
    methods: {
        categoryBar (item) {
            this.currentPage = 1;
            this.auditType = item.mark;
            this.queryPageList();
        },
        operationChange (item) {
            this.$router.push({ path: item.value });
            this.queryPageList();
        },
        //查询可选批次
        queryBatchMange () {
            BatchMangeApi.queryByCondition({ status: "UN_COMPLETE" }).then(
                (res) => {
                    this.batchList = res.data;
                }
            );
        },
        //查询可选物料
        queryMaterial () {
            MaterialApi.queryByCondition().then((res) => {
                this.materialList = res.data;
            });
        },
        //查询可选鱼塘
        queryFishPond () {
            FishPondApi.queryByCondition().then((res) => {
                this.pondList = res.data;
            });
        },
        //查询经手人
        queryStaff () {
            StaffApi.queryByCondition().then((res) => {
                this.staffList = res.data;
            });
        },
        queryEnumList () {
            EnumApi.list({ enumType: "AuditType" }).then((res) => {
                if (res.data) {
                    res.data.forEach((item, index) => {
                        this.categoryList.push({
                            id: index,
                            mark: item.name,
                            name: item.value,
                        });
                    });
                }
            });
        },
        queryPageList () {
            let params = {
                page: this.currentPage,
                size: this.pageSize,
                entity: {
                    auditType: this.auditType,
                },
            };
            if (this.mnecode) params.entity.mnecode = this.mnecode;
            if (this.fishPondId) params.entity.fishPondId = this.fishPondId;
            if (this.staffName) params.entity.staffName = this.staffName;
            if (this.inputDate) params.entity.inputDate = this.inputDate;
            HangingOperationApi.queryPageByCondition(params).then((res) => {
                if (res.success === true) {
                    let data = res.data || {};
                    this.totalSize = data.totalElements;
                    // 赋值数组
                    this.tableData = data.content;
                }
            });
        },
        changeCurrentPage (currentPage) {
            this.currentPage = currentPage;
            this.queryPageList();
        },
        selectAll (list) {
            let notvalue = true;
            list.forEach((item) => {
                if (!item.hangNetWorkId) {
                    notvalue = false;
                }
            });
            if (notvalue === false) {
                this.$message.warning("不可选择空数据！");
            }
        },
        select (list) {
            let notvalue = true;
            list.forEach((item) => {
                if (!item.hangNetWorkId) {
                    notvalue = false;
                }
            });
            if (notvalue === false) {
                this.$message.warning("不可选择空数据！");
            }
        },
        handleSelectionChange (list) {
            this.checkedIdList = [];
            let notvalue = true;
            let emptyList = [];
            this.checkedIdList = list.map((item) => {
                if (item.hangNetWorkId) {
                    return item.hangNetWorkId;
                } else {
                    notvalue = false;
                    emptyList.push(item);
                    return "";
                }
            });
            if (notvalue === false) {
                // this.$message.warning("不可选择空数据！");
                this.toggleNoSelection(emptyList);
                return;
            }
            if (this.checkedIdList.length >= 1) this.delActive = true;
            else this.delActive = false;
        },
        cancelPassDialog () {
            this.showDialog = false;
        },
        showAuditDialog (row, type) {
            this.auditedId = row.hangNetWorkId;
            this.nowChanceStau = type;
            this.dialogParams = row;
            this.showDialog = true;
        },
        submitAudite (data) {
            let params = {};
            if (this.nowChanceStau === "pass") {
                params = {
                    // 审核
                    auditedId: this.auditedId,
                    auditStaffId: sessionStorage.getItem("staffId"),
                    auditStaffName: sessionStorage.getItem("staffName"),
                    auditType: "PASS",
                };
            } else if (this.nowChanceStau === "unpass") {
                params = {
                    auditedId: this.auditedId,
                    auditStaffId: sessionStorage.getItem("staffId"),
                    auditStaffName: sessionStorage.getItem("staffName"),
                    auditType: "NOT_PASS",
                    failReason: data.failReason,
                };
            }
            HangingOperationApi.audit(params).then((res) => {
                if (res.success) {
                    this.$message.success("操作通过");
                    this.queryPageList()
                } else {
                    this.$message.info(res.message ? res.message : "系统错误,请稍后再试");
                }
                this.showDialog = false;
            });
        },
    },
};
</script>

<style lang="scss" scoped></style>
